package shark.picture.utils.miscellaneous;

import cn.hutool.core.img.ColorUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

// 生成简易图片
public class SimpleImageGenerator {

    public static void main(String[] args) {
        generateImage(
                "SM4转换", // 图片上的文字
                ColorUtil.hexToColor("#FFFAFA"), // 背景颜色
                "SM4.png" // 输出文件名
        );
    }

    /**
     * 生成自动换行并居中的文字图片
     */
    public static void generateImage(String text, Color bgColor, String fileName) {
        try {
            BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = image.createGraphics();

            // 设置抗锯齿和渲染参数
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            // 绘制背景
            g2d.setColor(bgColor);
            g2d.fillRect(0, 0, 200, 200);

            // 设置字体和颜色
            Font font = new Font("微软雅黑", Font.BOLD, 40);
            g2d.setFont(font);
            g2d.setColor(Color.BLACK);

            FontMetrics metrics = g2d.getFontMetrics();
            int maxWidth = 190; // 允许的最大行宽

            // 拆分文本为多行
            List<String> lines = splitIntoLines(text, metrics, maxWidth);

            // 计算垂直居中位置
            int totalHeight = lines.size() * metrics.getHeight();
            int startY = (200 - totalHeight) / 2 + metrics.getAscent();

            // 逐行绘制文字
            for (int i = 0; i < lines.size(); i++) {
                String line = lines.get(i);
                int lineWidth = metrics.stringWidth(line);
                int x = (200 - lineWidth) / 2;
                int y = startY + i * metrics.getHeight();
                g2d.drawString(line, x, y);
            }

            g2d.dispose();
            ImageIO.write(image, "PNG", new File("C:\\Users\\xiechang\\Desktop\\" + fileName));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 将文本拆分为适合宽度的多行
     */
    private static List<String> splitIntoLines(String text, FontMetrics metrics, int maxWidth) {
        List<String> lines = new ArrayList<>();
        int currentPosition = 0;
        int textLength = text.length();

        while (currentPosition < textLength) {
            int endPosition = findLineEnd(text, currentPosition, metrics, maxWidth);
            lines.add(text.substring(currentPosition, endPosition));
            currentPosition = endPosition;
        }
        return lines;
    }

    /**
     * 查找每行的结束位置
     */
    private static int findLineEnd(String text, int start, FontMetrics metrics, int maxWidth) {
        int end = start + 1;
        while (end <= text.length()) {
            String segment = text.substring(start, end);
            if (metrics.stringWidth(segment) > maxWidth) {
                return end - 1; // 返回上一个有效位置
            }
            end++;
        }
        return text.length(); // 最后剩余部分
    }
}
